<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="名称"
        style="width: 180px;"
        clearable
        class="filter-item"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <div style="display:inline-block;">
        <router-link :to="{path:`/sys/apitype/add`}">
          <el-button
            v-if="isP('SysApitypeAdd')"
            class="filter-item light-primary-btn"
            style="margin-left: 10px;"
            type="primary"
            icon="el-icon-plus"
          >
            新增
          </el-button>
        </router-link>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="name"
        label="名称"
        width="100"
      />
      <el-table-column
        prop="remarks"
        label="说明"
        width="400"
      />
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button v-if="isP('SysApitypeUpdate')" style="margin-left: 10px" type="primary" size="mini" @click="handleUpdate(scope)">修改</el-button>
          <el-button v-if="isP('SysApitypeDelete')" style="margin-left: 10px" type="danger" size="mini" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="listSysApitype"
    />
  </div>
</template>

<script>
import { listSysApitype, deleteSysApitype } from '@/api/sys/apitype'
import Pagination from '@/components/Pagination'
import msgUtil from '@/utils/msgUtil'
export default {
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: JSON.parse(localStorage.getItem('SysApitypeQuery')) || {
        name: '',
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.listSysApitype()
  },
  methods: {
    listSysApitype() {
      this.listLoading = true
      listSysApitype(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      })
      localStorage.setItem('SysApitypeQuery', JSON.stringify(this.listQuery))
    },
    // 按钮查询
    handleFilter() {
      this.listQuery.page = 1
      this.listSysApitype()
    },
    // 新增
    handleAdd() {
      // this.$router.push({ path: '/api/add' }) // 带参跳转
    },
    // 删除
    handleDelete({ $index, row }) {
      msgUtil.confirm('确认删除接口类型?').then(async() => {
        await deleteSysApitype({ id: row.id })
        msgUtil.success2('删除成功!')
        this.listSysApitype()
      })
    },
    // 处理修改
    handleUpdate({ $index, row }) {
      this.$router.push({
        path: '/sys/apitype/update',
        query: { id: row.id }
      })
    }
  }
}
</script>
